{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PS MIO\n",
    "1. 使用[pynq.gpio Module](https://pynq.readthedocs.io/en/latest/pynq_package/pynq.gpio.html)来控制PS的MIO\n",
    "2. 这部分不使用PL部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T13:38:15.081029Z",
     "start_time": "2020-05-19T13:38:13.798631Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq import GPIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T13:40:14.213142Z",
     "start_time": "2020-05-19T13:40:14.198707Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ultra96-V2 gpio_base is 338\n"
     ]
    }
   ],
   "source": [
    "gpio_base = GPIO.get_gpio_base()\n",
    "print(\"Ultra96-V2 gpio_base is {}\".format(gpio_base))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T13:42:33.396191Z",
     "start_time": "2020-05-19T13:42:33.382480Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ultra96-V2 gpio_base_path is /sys/class/gpio/gpiochip338\n"
     ]
    }
   ],
   "source": [
    "gpio_base_path = GPIO.get_gpio_base_path()\n",
    "print(\"Ultra96-V2 gpio_base_path is {}\".format(gpio_base_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T13:46:16.948073Z",
     "start_time": "2020-05-19T13:46:16.934703Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ultra96-V2 gpio_npins is 174\n"
     ]
    }
   ],
   "source": [
    "gpio_npins = GPIO.get_gpio_npins()\n",
    "print(\"Ultra96-V2 gpio_npins is {}\".format(gpio_npins))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T14:21:07.191190Z",
     "start_time": "2020-05-19T14:21:06.622405Z"
    }
   },
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "[Errno 16] Device or resource busy",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[0;31mOSError\u001b[0m: [Errno 16] Device or resource busy",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-22b01faa7ec5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mled1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mGPIO\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgpio_base\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m19\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'out'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/pynq/gpio.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, gpio_index, direction)\u001b[0m\n\u001b[1;32m    192\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    193\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_impl\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_impl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_exported\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 194\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_impl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_GPIO\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgpio_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdirection\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    195\u001b[0m             \u001b[0m_gpio_map\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mgpio_index\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_impl\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    196\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/pynq/gpio.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, gpio_index, direction)\u001b[0m\n\u001b[1;32m     77\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexists\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     78\u001b[0m             \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/sys/class/gpio/export'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 79\u001b[0;31m                 \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     80\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     81\u001b[0m         \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'direction'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mOSError\u001b[0m: [Errno 16] Device or resource busy"
     ]
    }
   ],
   "source": [
    "led1 = GPIO(gpio_base + 19, 'out')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T14:43:58.038673Z",
     "start_time": "2020-05-19T14:43:58.026700Z"
    }
   },
   "outputs": [],
   "source": [
    "gpio0 = GPIO(gpio_base + 36, 'out')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T14:54:07.252145Z",
     "start_time": "2020-05-19T14:53:47.171436Z"
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "for i in range(100):\n",
    "    gpio0.write(1)\n",
    "    time.sleep(0.1)\n",
    "    gpio0.write(0)\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T15:27:35.541636Z",
     "start_time": "2020-05-19T15:27:35.530864Z"
    }
   },
   "outputs": [],
   "source": [
    "gpio0.release()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PS EMIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T15:27:12.637628Z",
     "start_time": "2020-05-19T15:27:10.979432Z"
    }
   },
   "outputs": [],
   "source": [
    "from pynq import GPIO\n",
    "from pynq import Overlay\n",
    "import time\n",
    "emio_gpio_design = Overlay(\"./data/1.PS_EMIO.bit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T15:28:17.443265Z",
     "start_time": "2020-05-19T15:28:17.423470Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ultra96-V2 hd_emio_gpio0 numbers is 416\n"
     ]
    }
   ],
   "source": [
    "hd_emio_gpio0_n = GPIO.get_gpio_pin(gpio_user_index= 0)\n",
    "print(\"Ultra96-V2 hd_emio_gpio0 numbers is {}\".format(hd_emio_gpio0))\n",
    "hd_emio_gpio0 = GPIO(hd_emio_gpio0_n, 'out')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T15:29:58.422279Z",
     "start_time": "2020-05-19T15:29:38.343304Z"
    }
   },
   "outputs": [],
   "source": [
    "for i in range(100):\n",
    "    hd_emio_gpio0.write(1)\n",
    "    time.sleep(0.1)\n",
    "    hd_emio_gpio0.write(0)\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-19T15:32:54.897846Z",
     "start_time": "2020-05-19T15:32:54.887030Z"
    }
   },
   "outputs": [],
   "source": [
    "hd_emio_gpio0.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
